package com.yssoft.service.special;

import cn.hutool.core.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yssoft.entity.core.TCode;
import com.yssoft.entity.core.TDdDetail;
import com.yssoft.entity.special.TsPersonnelEntity;
import com.yssoft.entity.special.TsQualificationProofEntity;
import com.yssoft.mapper.core.TCodeMapper;
import com.yssoft.mapper.core.TDdDetailMapper;
import com.yssoft.mapper.special.TsPersonnelMapper;
import com.yssoft.utils.UploadFile;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 组织人员Service接口
 *
 * @author: ZhouZhou
 * @date 2025-07-26 18:05:22
 */
@Service
public class TsPersonnelService extends ServiceImpl<TsPersonnelMapper, TsPersonnelEntity> {

    @Resource
    private TsPersonnelMapper tsPersonnelMapper;
    @Resource
    private TCodeMapper codeMapper;
    @Resource
    private TDdDetailMapper ddDetailMapper;

    /**
     * mybaitis-plus 单表页面分页查询
     */
	/*public PageUtil findPage(Map<String, Object> params) {
		QueryWrapper<TsPersonnelEntity> queryWrapper = new QueryWrapper<TsPersonnelEntity>();
		String companyId = (String) params.get("companyId");
		String deleteFlag = (String) params.get("deleteFlag");
		String productName = (String) params.get("productName");
        queryWrapper.like(ToolUtil.isNotEmpty(productName), "product_name", productName);
		String approvalNumber = (String) params.get("approvalNumber");
		String standardNumber = (String) params.get("standardNumber");
		String productionProcess = (String) params.get("productionProcess");
		String deleteTime = (String) params.get("deleteTime");
		String createId = (String) params.get("createId");
		String updateId = (String) params.get("updateId");
		String deleteId = (String) params.get("deleteId");
		Page<TsPersonnelEntity> page = tsPersonnelMapper.selectPage(new Query<TsPersonnelEntity>(params).getPage(), queryWrapper);
		return new PageUtil(page);
    }*/

    /**
     * 自定义分页查询，含关联实体对像
     */
	/*public PageUtil findPage(Map<String, Object> params,TsPersonnelEntity tsPersonnelEntity) {
		params.put("__limit", "10000");
		params.put("__page", "0");
		tsPersonnelEntity.setCompanyId(Long.parseLong(tsPersonnelEntity.getId()));

		Page<TsPersonnelEntity> page = tsPersonnelMapper.findPage(new Query<TsPersonnelEntity>(params).getPage(),
					tsPersonnelEntity,
				(String) params.get(Constant.SQL_FILTER));
		return new PageUtil(page);
	}*/

    /**
     * 查列表
     */
    public List<TsPersonnelEntity> findList(TsPersonnelEntity tsPersonnelEntity) {
        return tsPersonnelMapper.findList(tsPersonnelEntity);
    }

    /**
     * 批量删除
     *//*
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteBatchByIds(Long[] ids) {
		return this.removeByIds(Arrays.asList(ids));
	}

	*//**
     * 单个删除
     *//*
	@Transactional(rollbackFor = Exception.class)
	public boolean delTsPersonnelById(String id) {
		return this.removeById(id);
	}

	*//**
     * 保存
     *//*
	@Transactional(rollbackFor = Exception.class)
	public boolean addTsPersonnel(TsPersonnelEntity tsPersonnel){
		tsPersonnel.setId(UUID.randomUUID().toString().replaceAll("-", ""));
*//*		LoginUserEntity loginUser = UserUtils.getUserInfo();

		tsPersonnel.setCreateBy(loginUser.getId().toString());
*//*
		int insert = tsPersonnelMapper.insert(tsPersonnel);
		return insert > 0;
    }

	*//**
     * 修改根居ID
     *//*
	@Transactional(rollbackFor = Exception.class)
	public boolean updateTsPersonnelById(TsPersonnelEntity tsPersonnel) {
		if(this.updateById(tsPersonnel)){
			return true;
		}
		return false;
	}

	*//**
     * 根居ID获取对象
     *//*
	public TsPersonnelEntity findTsPersonnelById(String id){
		return tsPersonnelMapper.findTsPersonnelById(id);
	}

	*//**
     * 软删除
     *//*
	@Transactional(rollbackFor = Exception.class)
	public boolean delete(TsPersonnelEntity entity) {
		LambdaUpdateWrapper<TsPersonnelEntity> wrapper = new LambdaUpdateWrapper<>();
		wrapper.eq(TsPersonnelEntity::getId, entity.getId()) // 根据 id 匹配
				.set(TsPersonnelEntity::getDeleteFlag, "1"); // 设置 deleteId 的值

		return this.update(wrapper);
	}*/

    /**
     * 保存
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean addTsPersonnel(TsPersonnelEntity entity, MultipartFile certificateFile, MultipartFile file) {
        entity.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        //根据字典表ID查询证件类型字典信息
        TCode cardTypeCode = codeMapper.queryCodeById(Integer.parseInt(entity.getCardTypeCode()));
        entity.setCardTypeCode(cardTypeCode.getCode());
        entity.setCardType(cardTypeCode.getDescribed());
        //根据字典表ID查询职位字典信息
        TDdDetail ddDetail = ddDetailMapper.queryDetailInfoById(Integer.parseInt(entity.getPostCode()));
        entity.setPostCode(ddDetail.getId().toString());
        entity.setPost(ddDetail.getDescribed());
        //根据字典ID查询职务信息
        TCode job = codeMapper.queryCodeById(Integer.parseInt(entity.getJobTitleCode()));
        entity.setJobTitleCode(job.getCode());
        entity.setJobTitle(job.getName());
        //根据字典ID学历信息
        TCode background = codeMapper.queryCodeById(Integer.parseInt(entity.getBackgroundCode()));
        entity.setBackgroundCode(background.getCode());
        entity.setEducationBackground(background.getDescribed());
        if (!certificateFile.isEmpty() && !file.isEmpty()) {
            Map<String, Object> certificateMap = UploadFile.compressUploadedFile(certificateFile);
            entity.setCertificate(certificateMap.get("fileName").toString());
            entity.setAppointmentDocument(UploadFile.processUploadedFile(file));
        }

        if (this.save(entity)) {
            return true;
        }
        return false;
    }

    /**
     * 修改根居ID
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean update(TsPersonnelEntity entity, MultipartFile certificateFile, MultipartFile file) {
        //根据字典表ID查询证件类型字典信息
        TCode cardTypeCode = codeMapper.queryCodeById(Integer.parseInt(entity.getCardTypeCode()));
        entity.setCardTypeCode(cardTypeCode.getCode());
        entity.setCardType(cardTypeCode.getDescribed());
        //根据字典表ID查询职位字典信息
        TDdDetail ddDetail = ddDetailMapper.queryDetailInfoById(Integer.parseInt(entity.getPostCode()));
        entity.setPostCode(ddDetail.getId().toString());
        entity.setPost(ddDetail.getDescribed());
        //根据字典ID查询职务信息
        TCode job = codeMapper.queryCodeById(Integer.parseInt(entity.getJobTitleCode()));
        entity.setJobTitleCode(job.getCode());
        entity.setJobTitle(job.getDescribed());
        //根据字典ID学历信息
        TCode background = codeMapper.queryCodeById(Integer.parseInt(entity.getBackgroundCode()));
        entity.setBackgroundCode(background.getCode());
        entity.setEducationBackground(background.getDescribed());


        if (!certificateFile.isEmpty()) {
            Map<String, Object> certificateMap = UploadFile.compressUploadedFile(certificateFile);
            entity.setCertificate(certificateMap.get("fileName").toString());

        }
        if (!file.isEmpty()) {
            entity.setAppointmentDocument(UploadFile.processUploadedFile(file));
        }

        if (this.updateById(entity)) {
            return true;
        }
        return false;
    }

    /**
     * 软删除
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(TsPersonnelEntity entity) {
        entity.setDeleteFlag(1);
        return this.updateById(entity);
    }

}
